package di

import (
	"context"
	"time"

	"kdemo/internal/service"

	"github.com/go-kratos/kratos/pkg/log"
	"github.com/go-kratos/kratos/pkg/net/rpc/warden"
	"github.com/gogf/gf/net/ghttp"
)

//go:generate kratos tool wire
type App struct {
	svc  *service.Service
	http *ghttp.Server
	grpc *warden.Server
}

func NewApp(svc *service.Service, h *ghttp.Server, g *warden.Server) (app *App, closeFunc func(), err error) {
	app = &App{
		svc:  svc,
		http: h,
		grpc: g,
	}
	closeFunc = func() {
		ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
		if err := g.Shutdown(ctx); err != nil {
			log.Error("grpcSrv.Shutdown error(%v)", err)
		}
		// if err := h.Start(ctx); err != nil {
		// 	log.Error("httpSrv.Shutdown error(%v)", err)
		// }
		cancel()
	}
	return
}
